module.exports = {
	renderAction(){
		let btnTpl = `<button 
			data-action="#{data.action}" 
			data-arg="#{data.arg}"
			data-dtype="#{data.dtype}"
			data-etype="#{data.etype}"
			data-toggle="#{data.toggle}"
			data-nofocus="#{data.noFocus}"
			title="#{data.title}" 
			class="#{data.btype} geoicon #{data.icon}"></button>`

		let ipTpl = `<input 
			data-action="#{data.action}"
			data-etype="#{data.etype}"
			class="${CN.input}">`

		let {actions,renderTo} = this

		this.$actionDom = document.createDOM("div",{
			class:[(this.cn.root||CN.pluginWrap),this.cn.cla||""].join(" ")
		})
		actions.forEach(data=>{
			let isInput = data.ttype==="input"
			let tpl = isInput?ipTpl:btnTpl
			let d = isInput?data:data.assign({icon:Icon[data.icon]})
			d.btype = CN[d.btype]||CN.btn
			let vdom = document.createElement("div")
				vdom.innerHTML = Utils.parseText(tpl,d)
			let actionBtn = vdom.firstElementChild
			if(data.options){
				for(let ko in data.options)
					actionBtn[ko] = data.options[ko]
			}
			this.$actionDom.appendChild(actionBtn)
			this.actionBtns[data.action] = actionBtn
		})
		
		this.$event.emit(EN[renderTo],this.$actionDom)
	},
	bind(){
		if(this.globalWatch)
			this.$event.on(EN.action,(nav)=>{
				this[nav.action]&&this[nav.action](nav)
			})
		this.on(Cst.focusIn,(...e)=>{
			this.$actionDom.classList.remove(CN.disabled)
			this.onFocusIn&&this.onFocusIn(...e)
		})
		this.on(Cst.focusOut,(...e)=>{
			this.$actionDom.classList.add(CN.disabled)
			this.onFocusOut&&this.onFocusOut(...e)
		})
		;["change","click"].forEach(en=>{
			this.$actionDom.addEventListener(en,(e)=>{
				this.onEvent.functional(this,e)
			})
		})

	},
	onEvent(e){
		let t = e.target
		let action = t.dataset.action
		if(!action)return
		if(e.type==="click"&&t.tagName==="BUTTON")
			this.onActionClick(e)
		if(e.type==="change")
			this.onActionChange(e)
	},
	onActionChange(e){
		let t = e.target;
		let {actions} = this
		let {action,etype} = t.dataset
		
		if(action.match(/EN\./)){
			action = action.split(".")[1]
			action = EN[action]
		}
		let data = {
			action,
			value : t.nvalue
		}
		if(etype==="inner"){
			this.emit(data.action,data)
		}else
			this.$event.emit(EN.action,data)
	},
	onActionClick(e){
		let t = e.target;
		let {actions} = this
		let {arg,action,dtype,etype,toggle,nofocus} = t.dataset
		let isActive = t.classList.contains(CN.active)
		//如果满足条件，直接以此广播
		let directEN = action.match(/EN\./)
		nofocus = Utils.dataTransform(nofocus,"boolean")
		if(directEN){
			action = action.split(".")[1]
			action = EN[action]
		}

		let data = {
			action
		}
		if(Utils.dataTransform(toggle,"boolean")){
			if(!nofocus)
				t.classList[isActive?"remove":"add"](CN.active)
			data.isActive = !isActive
			this.flag=!isActive
		}else {
			if(!nofocus){
				let prev = this.$actionDom.querySelector("."+CN.active)
				prev&&prev.classList.remove(CN.active)
				t.classList.add(CN.active)
			}
			data.isActive = true
			data.arg = Utils.dataTransform(arg,dtype)
		}
		if(etype==="inner"){
			this.emit(data.action,data,t)
		}else
			this.$event.emit(directEN?data.action:EN.action,data)
		this[action]&&this[action](t,data)
	}
}
	